package com.shouhe.mysap.controller.plan;


import com.shouhe.mysap.common.MySapResult;
import com.shouhe.mysap.common.PageResult;
import com.shouhe.mysap.common.status.PlanStatus;
import com.shouhe.mysap.common.status.PurStatus;
import com.shouhe.mysap.entity.PlanPlace;
import com.shouhe.mysap.entity.PurOrder;
import com.shouhe.mysap.service.PlanPlaceService;
import com.shouhe.mysap.service.PlanSaleOrderService;
import com.shouhe.mysap.service.PurOrderService;
import com.shouhe.mysap.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 守鹤
 * @since 2020-06-05
 */
@RestController
@RequestMapping("/plan-place")
public class PlanPlaceController {


    @Autowired
    private PlanPlaceService planPlaceService;
    @Autowired
    private PurOrderService purOrderService;

    /**
     * @描述 得到供应链销售订单
     * @参数
     * @返回值
     */
    @RequestMapping("/getPlanPlaces")
    public PageResult getPlanPlaces(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows) {
        PageResult pageResult = PageResultUtil.getPageResult(page, rows, planPlaceService);
        return pageResult;
    }


    /**
     * @描述 修改未下销售需求订单物料
     * @参数
     * @返回值
     */
    @RequestMapping("/updatePlanPlace")
    public MySapResult updatePlanPlace(PlanPlace planPlace) {
        return UpdateUtil.updateObjectById("修改未下销售需求订单物料", planPlace, planPlaceService);
    }


    /**
     * @描述 供应链下单给采购
     * @参数
     * @返回值
     */
    @RequestMapping("/addPlanPlaceToPurOrder")
    public MySapResult addPlanPlaceToPurOrder(@RequestParam("ids[]") Integer[] ids) {
        List<PlanPlace> list = new ArrayList<>();
        //更新PlanPlace状态为 已下单
        for (Integer id : ids) {
            PlanPlace planPlace = new PlanPlace().setId(id).setStatus(PlanStatus.PLAN_PLACE_HAS_PLANPLACE);
            list.add(planPlace);
        }
        MySapResult result = UpdateUtil.updateObjectListById("更新PlanPlace状态为 已下单", list, planPlaceService);
        if (!result.isSuccess()) {
            return result;
        }

        //查询出全部下单的PlanPlace对象
        List<PlanPlace> planPlaces = SelectUtil.selectListIn("id", Arrays.asList(ids), planPlaceService);
        //初始行号
        int row=10;
        List<PurOrder> purOrders = new ArrayList<>();
        for (PlanPlace place : planPlaces) {
            PurOrder purOrder = new PurOrder();
            purOrder.setCreatePerson("供应链神秘人");
            purOrder.setNoReceiveCnt(place.getPlanCnt());
            //下单的时候还没逾期
            if(new Date().getTime()<place.getPlanTime().getTime()){
                purOrder.setOverTimeCnt(0);
                purOrder.setStatus(PurStatus.PUR_ORDER_NORMAL);
            }else{
                purOrder.setOverTimeCnt(place.getPlanCnt());
                purOrder.setStatus(PurStatus.PUR_ORDER_OVERTIME);
            }
            purOrder.setPlanCnt(place.getPlanCnt());
            purOrder.setPlanTime(place.getPlanTime());
            purOrder.setPurGroup(place.getPurGroup());
            purOrder.setPurOrderId(IDUtil.getNewPurOrderId());
            purOrder.setPurOrderRow(row);
            purOrder.setReceiveCnt(0);
            purOrder.setReceivePerson("");
            purOrder.setSupplierCode(place.getThingCode());
            purOrder.setSupplierName(place.getSupplierName());
            purOrder.setRemark(place.getRemark());
            purOrder.setThingCode(place.getThingCode());
            purOrder.setThingName(place.getThingName());
            purOrder.setUnit(place.getUnit());
            purOrders.add(purOrder);
            row+=10;
        }

        MySapResult result1 = AddUtil.addObjectList("新增采购订单", purOrders, purOrderService);
        return result1;
    }


}

